<?php
namespace App\Http\Controllers\Api;

use App\Http\Resources\Api\Users as UserCollection;
use App\Models\Tournament;
use App\Models\TournamentUser;
use App\Models\User;
use App\Models\UserCount;
use Illuminate\Http\Request;

class ScoreboardController extends Controller
{

    public function tournament(Request $request, $id)
    {
        $tournament = Tournament::findOrFail($id);

        // 获取我的排名
        $user = app('auth')->user();
        $userTournament = TournamentUser::where([
            'tournament_id' => $id,
            'user_id' => app('auth')->id(),
        ])->first();
        $myScore = $userTournament ? $userTournament->score : 0;

        $rank = TournamentUser::where('score', '>', $myScore)->orderBy('user_id')->count();
        $rank++;

        $tops = $others = [];
        $selectRaw = 'users.nickname,users.avatar,users.id,users.gender';
        if ($rank <= 100) {
            // 排名小于100，获取所有100名用户
            $tops = User::selectRaw($selectRaw)
                ->join('tournament_users', 'users.id', '=', 'tournament_users.user_id')
                ->orderByDesc('tournament_users.score')
                ->take(100)
                ->get()
                ->toArray();
        } else {
            // 排名大于100，获取前后15名及前15名用户
            $tops = User::selectRaw($selectRaw)
                ->join('tournament_users', 'users.id', '=', 'tournament_users.user_id')
                ->orderByDesc('tournament_users.score')
                ->take(30)
                ->get()
                ->toArray();

            //前15名用户
            $usersBefore = User::selectRaw($selectRaw)
                ->join('tournament_users', 'users.id', '=', 'tournament_users.user_id')
                ->where('tournament_users.score', '>=', $myScore)
                ->orderByDesc('tournament_users.score')
                ->orderBy('tournament_users.user_id')
                ->take(15)
                ->get()
                ->toArray();

            //后15名用户
            $usersAfter = User::selectRaw($selectRaw)
                ->join('tournament_users', 'users.id', '=', 'tournament_users.user_id')
                ->where('tournament_users.score', '<', $myScore)
                ->orderByDesc('tournament_users.score')
                ->orderBy('tournament_users.user_id')
                ->take(15)
                ->get()
                ->toArray();
            $others = $usersBefore + [$user] + $usersAfter;
        }
        return [
            'tops' => $tops,
            'others' => $others,
        ];
    }

    function global () {
        // 获取我的排名
        $user = app('auth')->user();
        $myTotalEarned = $user->user_count->total_earned;
        $rank = UserCount::where('total_earned', '>', $myTotalEarned)->orderBy('user_id')->count();
        $rank++;

        $tops = $others = [];
        $selectRaw = 'users.nickname,users.avatar,users.id,users.gender';
        if ($rank <= 100) {
            // 排名小于100，获取所有100名用户
            $tops = User::selectRaw($selectRaw)
                ->join('user_counts', 'users.id', '=', 'user_counts.user_id')
                ->orderByDesc('user_counts.total_earned')
                ->take(100)
                ->get()
                ->toArray();
        } else {
            // 排名大于100，获取前后15名及前15名用户
            $tops = User::selectRaw($selectRaw)
                ->join('user_counts', 'users.id', '=', 'user_counts.user_id')
                ->orderByDesc('user_counts.total_earned')
                ->take(30)
                ->get()
                ->toArray();

            //前15名用户
            $usersBefore = User::selectRaw($selectRaw)
                ->join('user_counts', 'users.id', '=', 'user_counts.user_id')
                ->where('user_counts.total_earned', '>=', $myTotalEarned)
                ->orderByDesc('user_counts.total_earned')
                ->orderBy('user_counts.user_id')
                ->take(15)
                ->get()
                ->toArray();

            //后15名用户
            $usersAfter = User::selectRaw($selectRaw)
                ->join('user_counts', 'users.id', '=', 'user_counts.user_id')
                ->where('user_counts.total_earned', '<', $myTotalEarned)
                ->orderByDesc('user_counts.total_earned')
                ->orderBy('user_counts.user_id')
                ->take(15)
                ->get()
                ->toArray();
            $others = $usersBefore + [$user] + $usersAfter;
        }
        return [
            'tops' => $tops,
            'others' => $others,
        ];

    }
}
